/**
 * Created by Administrator on 2017/2/28.
 */

import {allUrl} from '../scripts/url'
import {Toast,checkLogin,getFormatDateByLong} from '../scripts/tip'


    export const  initMyOrder =() => {

        var sinceId = -10;
        var pageSize = 10;

        $('.myOrder').dropload(
            {
                scrollArea: window,
                loadDownFn: function (me) {
                    sinceId += 10;
                    $.ajax({
                        type: 'GET',
                        url: '' + allUrl() + 'mall/queryOrderList?sinceId=' + sinceId + '&pagesize=' + pageSize ,
                        dataType: 'json',
                        success: function (data) {

                            checkLogin(data, false, '');

                            if(data.head.returnCode===0){
                                if(data.head.subCode===0){
                                    var result=[];
                                    if(data.body){
                                        result=data.body.orderList;
                                    }else {
                                        result=[]

                                    }
                                    getData(result,me,sinceId)

                                }else {
                                    Toast(data.head.subMsg,2000,'');
                                }
                            }else {
                                Toast(data.head.returnMsg,2000,'');
                            }
                        }
                    })
                }
            }
        )
    };


    function  getData(result,me,sinceId){

        var list=[];
        var order,product, orderId,orderNumber,totalPrice,status,productName,productId,productDetailId,size,color,count,productPrice,productUrl,actualPrice,statusText,className;
        var obj=$(".temp");

        if(sinceId===0){
           if( result.length<=0){
               $('.noneOrder').removeClass('hide');
           }else {
               $('.noneOrder').addClass('hide');
           }
        }

        if(result.length>0){

            for(var i=0;i<result.length;i++){

                order=result[i].order;
                product=result[i].product;
                orderId=order.orderId;
                orderNumber=order.orderNumber;
                totalPrice=order.totalPrice;
                actualPrice=order.actualPrice;
                status=order.status;
                productId=product.productId;

                if(status===0){
                    statusText='待付款';
                    className='waitPay';
                    obj.find(".cancelOrder").removeClass('hide');
                    obj.find(".payBtn").removeClass('hide').addClass('surePay').attr('href','#/mall/orderDetails?orderId='+orderId+'&orderNumber='+orderNumber+'').html('付款');
                }else {
                    obj.find(".cancelOrder").addClass('hide');
                    if(status===4){
                        statusText='已取消';
                        className='finishPay';
                        obj.find(".payBtn").removeClass('hide').addClass('cancelPay').removeClass('refundOrder').attr('href','#/mall/productDetails?productId='+productId+'').html('重新购买');
                    }else {
                        obj.find(".payBtn").addClass('hide');
                        if(status===1){
                            statusText='待发货';
                            className='finishPay';
                            obj.find(".payBtn").removeClass('hide').addClass('refundOrder').removeClass('cancelPay').attr('href','javascript:;').html('退款');
                        }else if(status===2){
                            statusText='已发货';
                            className='finishPay'
                        }else if(status===3){
                            statusText='已完成';
                            className='finishPay'
                        }else if(status===5){
                            statusText='已关闭';
                            className='waitPay'
                        }else if(status==6){
                            statusText='待审核';
                            className='waitPay'
                        } else {
                            statusText='已退款';
                            className='waitPay'
                        }
                    }
                }

                productName=product.productName;
                productId=product.productId;
                productDetailId=product.category.productDetailId;
                size=product.category.size;
                color=product.category.color;
                count=product.category.count;
                productPrice=product.category.productPrice;
                productUrl=product.category.productUrl;

                obj.find(".orderNum ").html('订单编号:'+orderNumber+'');
                obj.find(".cancelOrder ").attr('orderId',orderId);
                obj.find(".statusFw").addClass(''+className+'').html(statusText);
                obj.find(".payBtn").attr('orderId',orderId);
                obj.find(".product-img").css({"background-image":'url("'+productUrl+'")', "background-size": "100%  100%"});
                obj.find(".order-name h1").html(productName);
                obj.find(".colorSize").html(color+' '+size);
                obj.find(".order-price h1").html('￥'+productPrice+'');
                obj.find(".order-price h2").html('x'+count+'');
                obj.find(".count").html('共'+count+'件商品');
                obj.find(".realPrice").html('实付:￥'+actualPrice+'');
                obj.find(".orderDetails ").attr('href','#/mall/orderDetails?orderId='+orderId+'&orderNumber='+orderNumber+'');

                list.push(obj.html());

                obj.find(".statusFw").hasClass('waitPay');
                obj.find(".statusFw").removeClass('finishPay');
            }
        }else {
            me.lock();

            me.noData();
        }

        $('.MyOrderList ul').append(list);


        $('.cancelOrder').click(function(){

            var $orderId=$(this).attr('orderId');

            layer.open({
                content: '<div class="cancelTip">确定取消吗？</div>',
                style: 'position:fixed; top:35%; width:80%; left:50%;margin-left:-40%;border-radius: 8px;background:#fff',
                btn: ['确定', '取消'],
                yes: function(index){
                    cancelOrder($orderId);
                    location.reload();
                    layer.close(index);
                }
            });
         });

        $('.refundOrder').click(function(){
            var $orderId=$(this).attr('orderId');
            layer.open({
                content: '<div class="cancelTip">确定退款吗？</div>',
                style: 'position:fixed; top:35%; width:80%; left:50%;margin-left:-40%;border-radius: 8px;background:#fff',
                btn: ['确定', '取消'],
                yes: function(index){
                    refundOrder($orderId);
                    location.reload();
                    layer.close(index);
                }
            });
        });

        var ua = navigator.userAgent.toLowerCase();

        $('.contactService').click(function(){
            if (ua.indexOf("android") > -1) {
                //android版本
                JsObject.onTel(String(4008659517));

            } else if (ua.indexOf("ipad") > -1 || ua.indexOf("iphone os") > -1) {
                //ios版本
                $(this).attr('href','tel:4008659517');

            }
        });

        me.resetload();
    }



   //TODO  取消订单
    function cancelOrder(orderId){

        $.post(''+allUrl()+'mall/cancelCustomerOrder?orderId='+orderId+'&reason=',  (data) => {

            data=$.parseJSON(data);

            checkLogin(data, false, '');

            if(data.head.returnCode===0){

                if(data.head.subCode===0){

                }else {
                    Toast(data.head.subMsg,2000,'');
                }
            }else {
                Toast(data.head.returnMsg,2000,'');
            }
        })
    }

    //TODO  订单退款
    function refundOrder(orderId){

        $.post(''+allUrl()+'mall/refundOrder?orderId='+orderId+'&reason=', (data) => {

            data=$.parseJSON(data);

            checkLogin(data, false, '');

            if(data.head.returnCode===0){

                if(data.head.subCode===0){

                    $('.refundOrder').addClass('hide')

                }else {
                    Toast(data.head.subMsg,2000,'');
                }
            }else {
                Toast(data.head.returnMsg,2000,'');
            }
        })
    }